# Modified from https://gitlab.archlinux.org/archlinux/packaging/packages/micro/-/blob/dffc678799d1ade1edd55aec66f9312a3d4c8ea3/PKGBUILD

_realname=micro
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.0.14
pkgrel=1
# manually set date instead of "go run tools/build-date.go", for reproducible builds
_build_date='August 27, 2024'
pkgdesc='Modern and intuitive terminal-based text editor'
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://micro-editor.github.io/'
msys2_repository_url='https://github.com/zyedidia/micro'
license=('spdx:MIT')
makedepends=(git "${MINGW_PACKAGE_PREFIX}-go" "${MINGW_PACKAGE_PREFIX}-cc")
source=("git+https://github.com/zyedidia/micro#commit=04c577049ca898f097cd6a2dae69af0b4d4493e1") # tag: v2.0.14
b2sums=('6c1d0fbf3b6830de95cc5bf1bd3905af36dc7a204fc353c8424848015c7ef6056e6d803e998f8dc397322168dc83a2861c75af29ff846c50435a2e5524c1632c')

prepare() {
  cd $_realname
  # Let tools/build-date.go always return the same date, to make the package reproducible
  echo -e '//+build ignore\npackage main\nimport "fmt"\nfunc main() { fmt.Println("'$_build_date'") }' > tools/build-date.go
  # Modify the Go build flags
  sed -i 's/-trimpath/-buildmode=pie -trimpath/g;s/GS)/GS) -linkmode=external -extldflags \\"$(LDFLAGS)\\"/g' Makefile
}

build() {
  make -C $_realname CC=cc
}

check() {
  make -C $_realname test
}

package() {
  cd $_realname
  install -Dm755 $_realname "$pkgdir/usr/bin/$_realname"
  install -Dm644 assets/packaging/micro.1 -t "$pkgdir/usr/share/man/man1"
  install -Dm644 assets/packaging/micro.desktop -t "$pkgdir/usr/share/applications"
  install -Dm644 assets/micro-logo-mark.svg "$pkgdir/usr/share/icons/hicolor/scalable/apps/$_realname.svg"
  install -Dm644 -t "$pkgdir/usr/share/licenses/$_realname" LICENSE LICENSE-THIRD-PARTY
}
